local redis = require "redis_iresty"
local cache = ngx.shared[ngx.var.nginx_cache]
local _M = {}

_M.new = function(self) 
  local red = redis:new() 
  self.red = red
  self.cache = cache
  self.cache_expire = tonumber(ngx.var.cache_expire)
  return self
end

_M.get = function(self, key)
  local result = self.cache:get(key) 
  if not result then 
    result = self.red:get(key)
    self.cache:set(key, result, self.cache_expire)
  end
  return result
end

_M.set = function(self, key, value) 
  self.cache:set(key, value, self.cache_expire)
  local resultRedis = self.red:set(key, value)
  return resultRedis
end

return _M
